/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author tien
 */
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

public class Moveable  extends BaseRMS {
    protected Sprite sprite;
    protected SpriteAnimationTask spriteAnimator;
    protected Controller controller;
    protected GameScreen canvas;
    protected final int speed = 2;
    protected int direction;
    protected int previousDirection;
    
    protected int[] downSeq;
    protected int downTrans;
    protected int[] upSeq;
    protected int upTrans;
    protected int[] leftSeq;
    protected int leftTrans;
    protected int[] rightSeq;
    protected int rightTrans;
    
    public Moveable(GameScreen canvas, Sprite sprite, SpriteAnimationTask animator) {
        this.canvas = canvas;
        this.sprite = sprite;
        this.spriteAnimator = animator;
    }

    public void setSequences(int[] downSeq, int downTrans, int[] upSeq, int upTrans, int[] leftSeq, int leftTrans, int[] rightSeq, int rightTrans) {
        this.downSeq = downSeq;
        this.downTrans = downTrans;
        this.upSeq = upSeq;
        this.upTrans = upTrans;
        this.leftSeq = leftSeq;
        this.leftTrans = leftTrans;
        this.rightSeq = rightSeq;
        this.rightTrans = rightTrans;
    }
    
    public void setDirection(int direction) {
        this.direction = direction;
        updateDirection();
    }
    
    public void setController(Controller controller) {
        this.controller = controller;
    }
    
    public int getX() {
        return sprite.getX();
    }
    
    public int getY() {
        return sprite.getY();
    }
    
    public int getWidth() {
        return sprite.getWidth();
    }
    
    public int getHeight() {
        return sprite.getHeight();
    }
    
    public Sprite getSprite() {
        return sprite;
    }

    int getLastDirection() {
        return previousDirection;
    }
    
    void update() {
    }

    void die() {
        removeAnimation();
    }
    
    private void removeAnimation() {
        this.canvas.getLayerManager().remove(this.sprite);
    }
    
    void stop() {
        this.spriteAnimator.setMoving(false);
    }
    
    void start() {
        this.spriteAnimator.setMoving(true);
    }

    void keepMoving() {
        switch (this.previousDirection) {
            case GameCanvas.UP:
                moveUp();
                break;
            case GameCanvas.DOWN:
                moveDown();
                break;
            case GameCanvas.LEFT:
                moveLeft();
                break;
            case GameCanvas.RIGHT:
                moveRight();
                break;
            default:
                //cannot rich here
                break;
        }
        //not necessary
        //updateDirection();
    }
    
    void recovery() {
        switch (this.previousDirection) {
            case GameCanvas.UP:
                moveDown();
                break;
            case GameCanvas.DOWN:
                moveUp();
                break;
            case GameCanvas.LEFT:
                moveRight();
                break;
            case GameCanvas.RIGHT:
                moveLeft();
                break;
            default:
                //cannot rich here
                break;
        }
        //not necessary
        //updateDirection();
    }

    void moveUp() {
        sprite.move(0, -speed);
    }

    void moveDown() {
        sprite.move(0, speed);
    }

    void moveLeft() {
        sprite.move(-speed, 0);
    }

    void moveRight() {
        sprite.move(speed, 0);
    }

    private void updateDirection() {
        this.previousDirection = this.direction;
    }
    
    boolean checkCollideAndSolve() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    boolean changeDirection() {
        return this.previousDirection != this.direction;
    }

    void turn() {
        switch (this.direction) {
            case GameCanvas.UP:
                turnUp();
                break;
            case GameCanvas.DOWN:
                turnDown();
                break;
            case GameCanvas.LEFT:
                turnLeft();
                break;
            case GameCanvas.RIGHT:
                turnRight();
                break;
            default:
                //cannot rich here
                break;
        }
        updateDirection();
    }
    
    void turnUp() {
        sprite.setFrameSequence(this.upSeq);
        sprite.setTransform(this.upTrans);
        
        boolean canMove = this.checkCollideAndSolve();
        if (!canMove) {
            recovery();
        }
    }

    void turnDown() {
        sprite.setFrameSequence(this.downSeq);
        sprite.setTransform(this.downTrans);
        
        boolean canMove = this.checkCollideAndSolve();
        if (!canMove) {
            recovery();
        }
    }

    void turnLeft() {
        sprite.setFrameSequence(this.leftSeq);
        sprite.setTransform(this.leftTrans);
        
        boolean canMove = this.checkCollideAndSolve();
        if (!canMove) {
            recovery();
        }
    }

    void turnRight() {
        sprite.setFrameSequence(this.rightSeq);
        sprite.setTransform(this.rightTrans);
        
        boolean canMove = this.checkCollideAndSolve();
        if (!canMove) {
            recovery();
        }
    }

    void goBack() {
        switch (this.previousDirection) {
            case GameCanvas.UP:
                moveDown();
                break;
            case GameCanvas.DOWN:
                moveUp();
                break;
            case GameCanvas.LEFT:
                moveRight();
                break;
            case GameCanvas.RIGHT:
                moveLeft();
                break;
            default:
                //cannot rich here
                break;
        }
        //not necessary
        //updateDirection();
    }

    void turnOtherWay() {
        this.direction = this.canvas.getOtherDirection(this.direction);
    }

    void fire() throws IOException, Exception {
        canvas.addBullet();
    }

    void loadData() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    void createDefaultData() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    void updateData() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
